/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import it.francescogabbrielli.android.solidalapp.SolidalApp.DataKey;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.util.Log;

public abstract class MessageActivity<D> extends BaseActivity implements LoaderCallbacks<D> {
	
	protected Menu menu;
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		this.menu = menu;
		updateMenu();
		return super.onCreateOptionsMenu(menu);
	}
		
	protected void updateMenu() {
		try {
			boolean enabled = app.getStatus().getGcmId()!=null;
			MenuItem item = menu.findItem(R.id.action_reload);
			item.setEnabled(enabled);
			item.getIcon().setAlpha(enabled ? 255 : 64);
		} catch(Exception e) {
			Log.w (SolidalApp.TAG, "Menu update error: "+e);
		}
	}
		
	@Override
	public void onStatusUpdate(StatusEvent event) {
		switch(event.getType()) {
		// Reload current activity data on GCM registration events
		case GENERIC:
			updateMenu();
			retryReload(false);
			break;
		case GCM:
			updateMenu();
			retryReload(true);
			break;
		default:
		}
	}
	
	@Override
	public void onDataChanged(final DataEvent event) {
		
		// Reverse address lookup on location change
		if(event.getKey()==DataKey.ADDRESS_LOCATION)
			app.addNetworkRetry(new RetryListener() {
				@Override
				public void retry() {
					if(app.getCurrentActivity()==MessageActivity.this) {
						Location loc = (Location) app.getData(event.getKey());
						new GetAddressTask(MessageActivity.this).execute(loc);
					}
				}
			});
	}	
	
	protected abstract int getLoaderId();
	
	/** 
	 * Reload data. Implements loader initialization
	 *  
	 * @param restart
	 * 					whether to restart the loader
	 */
	protected void reload(boolean restart) {
		String regId = app.getStatus().getGcmId();
		if (regId!=null) {
			Bundle filters = new Bundle();
			filters.putString("registrationId", regId);
			if (restart)
				getSupportLoaderManager().restartLoader(getLoaderId(), filters, this);
			else
				getSupportLoaderManager().initLoader(getLoaderId(), filters, this);
		} else {
			Loader<D> loader = getSupportLoaderManager().getLoader(getLoaderId());
			if (loader!=null)
				loader.reset();
		}
	}	
	
	/**
	 * Reload and if network is not available retry when it will become
	 * available
	 * 
	 * @param restart
	 * 					whether to restart the loader
	 */
	protected boolean retryReload(final boolean restart) {
		return app.addNetworkRetry(new RetryListener() {
			@Override
			public void retry() {
				if(app.getCurrentActivity()==MessageActivity.this)
					reload(restart);
			}
		});
	}
	
}
